package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.Admin;
import com.internship.model.DepartmentInfo;
import com.internship.model.TeacherInfo;
import com.internship.page.Page;
import com.internship.service.AdminService;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.TeacherInfoService;


@Controller
@RequestMapping("teacher")
public class TeacherController {
	private final Logger logger = Logger.getLogger(TeacherController.class);
	@Autowired
	private AdminService adminService;
	@Autowired
	private TeacherInfoService teacherInfoService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	/**?
	 * 教师信息列表
	 * @return
	 */
	@RequestMapping("/teacher_list.do")
	public ModelAndView getTeacherInfoList() {
		return getTeacherInfoList(null,null,1);
	}
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/teacher_page_list.do")
	public ModelAndView getTeacherInfoList(@RequestParam String BM,@RequestParam String XM,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("teacher_info");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<TeacherInfo> pageList = teacherInfoService.getTeacherInfoList(BM, XM, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("BM", BM);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("teacherList", pageList.getResultList());
			}else {
				Page<TeacherInfo> pageList = teacherInfoService.getTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)), XM, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("teacherList", pageList.getResultList());
			}
		mv.addObject("XM", XM);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 *实习教师管理列表
	 * @return
	 */
	@RequestMapping("/practice_teacher_list.do")
	public ModelAndView getPracticeTeacherInfoList() {
		return getPracticeTeacherInfoList(null,null,1);
	}
	@RequestMapping("/practice_teacher_page_list.do")
	public ModelAndView getPracticeTeacherInfoList(@RequestParam String BM,@RequestParam String XM,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("practice_teacher_info");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<TeacherInfo> pageList = teacherInfoService.getTeacherInfoList(BM, XM, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("BM", BM);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceTeacherList", pageList.getResultList());
			}else {
				Page<TeacherInfo> pageList = teacherInfoService.getTeacherInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)), XM, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("practiceTeacherList", pageList.getResultList());
			}
		mv.addObject("XM", XM);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 删除
	 * @param ZGH
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delTeacherInfo(@RequestParam String ZGH){
			JSONObject json = new JSONObject();
			int  rel= teacherInfoService.delTeacherInfo(ZGH);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
		/**?
		 * 指定为系院管理员
		 * @param teacherNumber
		 * @return
		 */
		@RequestMapping("/administrators.do")
		@ResponseBody
		public JSONObject administrators(@RequestParam String ZGH) {
			JSONObject json = new JSONObject();
		Admin admin = adminService.getAdminByteacherNumber(ZGH);
		if (admin ==null) {
			TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(ZGH);
			Admin admin2 = new Admin();
			admin2.setTeacherNumber(teacherInfo.getZGH());
			admin2.setTeacherName(teacherInfo.getXM());
			admin2.setPasswrod(teacherInfo.getPwd());
			DepartmentInfo departmentInfo =departmentInfoService.getDepartmentInfo(teacherInfo.getBM());
			admin2.setDeptNumber(departmentInfo.getXYDM());
			admin2.setDeptName(departmentInfo.getXYMC());
			admin2.setUserRole(3);
			int rel = adminService.addAdmin(admin2);
			json.put("result", rel > 0 ?"success":"error");
		}else {
			json.put("result", "is");
		}
			return json;
		}
}